<template>
	<view class="">
		<view class="content">
			<!-- 头部banner -->
			<!-- <detail-swiper :list="swiperList"></detail-swiper> -->
			
			<view class="top-bg">
				
			</view>
			
			<view class="content_info">

				<!-- 活动具体信息 -->
				<detail-info :activityTitle="activityInfo.activityName"
					:activiyTime="activityInfo.timeStart+'至'+activityInfo.timeEnd" :finishTime="activityInfo.timeEnd">

					<view class="other-content" slot="other">
						<image class="seckillTitleBg" src="../../static/activity/seckillBg.png" mode=""></image>
						
						<view class="seckillInfo">
							<text class="seckillTitle">秒杀价</text>
							<view class="nowPrice">¥
								<text style="font-size: 36upx;color: #ec4863;">{{activityInfo.nowPrice}}</text>
							</view>
							<text class="countNumber">仅剩{{activityInfo.countNumber - activityInfo.number}}份</text>
						</view>
						

						<view class="costPrice">¥{{activityInfo.costPrice}}</view>

					</view>
				</detail-info>

				<!-- 活动门店 -->
				<view v-if="activityInfo.unitDto">
					<home-title title="活动门店" type="2"></home-title>
					<view v-for="(item, index) in activityInfo.unitDto" :key="index">
						<store-cell :isLocation="0" :storeName="item.applicableUnit" :storeAddress="item.unitAddress"
							:distance="item.distance" :phone="item.unitPhone" :latitude="item.unitLat"
							:longitude="item.unitLon"></store-cell>
					</view>
				</view>

				<!-- 活动商品 -->
				<view v-if="activityInfo.goodsVoList">
					<home-title title="商品说明" type="2"></home-title>
					<view v-for="(item, index) in activityInfo.goodsVoList" :key="index" class="goods">
						<detail-commodity :commodityTitle="item.goodsServerName"
							:commodityDesc="item.goodsServerDescribe" :count="item.goodsServerNumber"
							:totalAmount="item.goodsServerPrice" :totalPrice="'{{item.goodsServerNumber * item.goodsServerPrice}}'"></detail-commodity>
					</view>
				</view>

				<!-- 活动介绍 -->
				<detail-introduce :richInfo="activityInfo.content"></detail-introduce>
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="bottomView">
			<view class="btn leftBtn">
				分享好友
				<button style="position: absolute; width: 100%; height: 100%; opacity: 0;" open-type="share"></button>
			</view>
			<view class="btn rightBtn" @click="confimOrder">
				立即抢购
			</view>
		</view>
		<login-mask></login-mask>
	</view>
</template>

<script>
	import detailSwiper from "../../components/activity/activity-detailSwiper.vue"
	import detailInfo from '../../components/activity/activity-detailInfo.vue'
	import homeTitle from '../../components/layout/mmv-home-title.vue'
	import storeCell from '../../components/coupon/store-cell.vue'
	import loginMask from '../../components/layout/login-mask.vue'
	import detailCommodity from '../../components/activity/activity-detailCommodity.vue'
	import detailIntroduce from '../../components/activity/activity-detailIntroduce.vue'
	import moment from '@/util/moment.js'

	import {
		apiActivityInfo,
		insertActivityBrowseRecords
	} from '../../common/api.js'
	import GLOBAL_CONFIG from '@/common/config';
	export default {
		components: {
			detailSwiper,
			detailInfo,
			homeTitle,
			storeCell,
			detailCommodity,
			detailIntroduce,
			loginMask
		},
		data() {
			return {
				time: 0,
				timeNumber: 0,
				lat: '',
				lon: '',
				activityId: '',
				activityInfo: {},
				swiperList: [],
				baseImageUrl: GLOBAL_CONFIG.yxBaseImageUrl,
			}
		},
		// onLoad(op) {
		// 	this.reckonBytime()
		// },
		beforeDestroy() {
			// this.insertActivityBrowseRecords()
			clearInterval(this.time)
		},
		methods: {
			insertActivityBrowseRecords() {
				insertActivityBrowseRecords({
					bizNo: this.activityId,
					memberNo: this.$store.state.user.userInfo.customerId,
					browseDuration: this.time,
					tenantId: this.$store.state.user.tenantCode,
					activityType: 3
				}).then(res => {})
			},
			reckonBytime() {
				this.time = setInterval(() => {
					this.timeNumber++
				}, 1000)
			},
			//获取定位信息
			getLocation() {
				uni.getLocation({
					type: 'wgs84',
					success: (res => {
						this.lat = res.latitude;
						this.lon = res.longitude;
						this.getData();
					}),
					fail:(e=>{
						this.getData();
					})
				});
			},
			//获取详情数据
			getData() {
				let that = this;
				apiActivityInfo({
					bizNo: that.activityId,
					lon: this.lon,
					lat: this.lat
				}).then(res => {
					this.activityInfo = res;
					if (this.activityInfo.detailsImage.length > 0) {
						let arr = this.activityInfo.detailsImage.split(',');
						this.swiperList = arr.map(item => {
							return GLOBAL_CONFIG.yxBaseImageUrl + item;
						})
					}
					
					//如果门店id是空，从详情获取使用门店第一个
					if (uni.$u.test.isEmpty(this.$store.state.user.deptId)) {
						getApp().globalData.tempDeptId = res.unitDto[0].applicableUnitNo;
						this.$store.commit('user/saveSourceType', 2);
						this.$store.commit('user/savedeptId', res.unitDto[0].applicableUnitNo);
					
						this.$store.dispatch('app/getStoreInfo', res.unitDto[0].applicableUnitNo);
					}
					
				})
			},

			onShareAppMessage(res) {
				this.$store.dispatch('home/recordShare')
				
				return {
					title: this.activityInfo.activityName,
					imageUrl: this.baseImageUrl + this.activityInfo.shareImage,
					path: '/pages/activity/activityDetail-secondKill?bizNo=' + this.activityId + '&deptId='+this.$store.state.user.deptId + '&customerId=' + this.$store.state.user.userInfo.customerId
				}
			},



			confimOrder() {
				if (uni.$u.test.isEmpty(this.$store.state.user.userInfo)) {
					this.$store.dispatch('user/MmvLogin');
					return
				}
				uni.navigateTo({
					url: './activity-confimOrder?bizNo=' + this.activityId
				})
			}
		},
		onLoad(op) {
			
			//记录开始时间
			getApp().globalData.taskStartTime = new Date().getTime();
			
			// console.log('开始时间', moment(getApp().globalData.taskStartTime).format('YYYY-MM-DD HH:mm:ss'));
			
			getApp().globalData.objType = 1;
			
			
			if(!uni.$u.test.isEmpty(op.scene)){
				const scene = decodeURIComponent(op.scene);
				let s_list = scene.split('=');
				console.log('op-scene=>', s_list);
				
				//任务id=推广人id=推广人类型=推广活动id
				getApp().globalData.taskObjId = s_list[0];
				getApp().globalData.personnelId = s_list[1];
				getApp().globalData.personnelType = s_list[2];
				op.bizNo = s_list[3];
			}
			
			this.activityId = op.bizNo;
			getApp().globalData.objId = this.activityId;
			
			
			console.log('收到的deptId=>', op.deptId);
			
			getApp().globalData.tempDeptId = op.deptId;
			
			if(!uni.$u.test.isEmpty(op.deptId)){
				this.$store.commit('user/saveSourceType', 4);
				this.$store.commit('user/savedeptId', op.deptId);
				
				this.$store.dispatch('app/getStoreInfo', op.deptId);
			}
			
			getApp().globalData.tempCustomerId = op.customerId;
			if(!uni.$u.test.isEmpty(op.customerId)){
				this.$store.commit('user/saveCustomerId', op.customerId);
			}
			
			
			this.getLocation();
			// this.reckonBytime()
			
			if(uni.$u.test.isEmpty(this.$store.state.user.openid)){
				//如果openid是空，可能是从扫码直接进入的，得等openid那个接口请求完了收到通知，再调操作记录接口
				uni.$on('getOpenIded', res=>{
					console.log('我收到openid了');
					
					//访客
					this.$store.dispatch('home/recordUse', {
						recordType:2,
					})
				})
			}else{
				//访客
				this.$store.dispatch('home/recordUse', {
					recordType:2,
				})
			}
			
			
			
		},
		onShareTimeline() {
			this.$store.dispatch('home/recordShare')
			
			return {
				title: this.activityInfo.luckdrawName,
				imageUrl: this.baseImageUrl + this.activityInfo.shareImage,
				path: '/pages/activity/activityDetail-secondKill?bizNo=' + this.activityId + '&deptId='+this.$store.state.user.deptId + '&customerId=' + this.$store.state.user.userInfo.customerId
			}
		},
		onUnload() {
			
			let nowTimes = new Date().getTime();
			
			let duration = Math.round((nowTimes - getApp().globalData.taskStartTime)/1000);
			
			
			//访问
			this.$store.dispatch('home/recordUse', {
				recordType:3,
				objRecordData:{
					durationTime:duration,
					recordTime:moment(nowTimes).format('YYYY-MM-DD HH:mm:ss')
				}
			})
		}

	}
</script>

<style lang="scss" scoped>
	.content {
		margin-bottom: 100upx;

		&_info {
			padding: 0 30upx;
			position: relative;
			top: -90upx;
		}
	}
	
	.top-bg{
		width: 100%;
		height: 152upx;
		background: linear-gradient(to bottom, #F5295E, #EF7091, #F8F8F8);
		
	}

	.secondKill {
		margin: 10upx 0;
		position: relative;
	}

	.seckillInfo {
		// position: relative;
		display: flex;
		justify-content: space-around;
		align-items: center;
		// vertical-align: bottom;
	}

	.seckillTitle {
		color: #fff;
		font-size: 22upx;
		z-index: 100;
		margin-left: 8upx;
	}

	.nowPrice {
		font-size: 22upx;
		font-family: PingFang SC, PingFang SC-Bold;
		font-weight: bold;
		font-style: italic;
		text-align: left;
		color: #ec4863;
		// margin-top: 23upx;
		margin-left: 15upx;
		margin-right: 23upx;
	}

	.countNumber {
		font-size: 24upx;
		font-family: PingFang SC, PingFang SC-Bold;
		// font-weight: bold;
		// text-align: left;
		color: #ec4863;
	}

	.costPrice {
		font-size: 28upx;
		font-family: PingFang SC, PingFang SC-Medium;
		// font-weight: bold;
		font-style: italic;
		// text-align: left;
		text-decoration: line-through;
		color: #999999;
		margin-top: 15upx;
	}
	
	.other-content{
		position: relative;
	}
	
	.seckillTitleBg {
		top: 6upx;
		left: 0;
		position: absolute;
		width: 86upx;
		height: 38upx;
	}

	.bottomView {
		display: flex;
		width: 100%;
		height: 96upx;
		line-height: 96upx;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 3;
	}

	.btn {
		width: 50%;
		line-height: 100%;
		font-size: 33upx;
		display: flex;
		justify-content: center;
		align-items: center;
		position: relative;
	}

	.leftBtn {
		color: #666;
		background-color: #FFFFFF;
		height: 96upx;
		line-height: 96upx;
		align-self: center;
		text-align: center;
	}

	.rightBtn {
		color: #fff;
		background-color: #F83C57;
	}
	
	.goods{
		padding-top: -20upx;
	}
</style>
